package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCarService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.ShoppingCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private ShoppingCartMapper cartMapper;
    /**
     * 增加购物车
     * */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        Long dishId=dto.getDishId();
        Long setmealId=dto.getSetmealId();
        Long userId= BaseContext.getCurrentId();
        //判断是否是首次添加商品
        ShoppingCart shoppingCart= shoppingCartMapper.selectByDishIdOrSetmealId(userId,dishId,setmealId);
        //是insert
        if(shoppingCart==null){
            //insert
             shoppingCart = new ShoppingCart();
            if(dishId!=null && dishId>0){
                //添加菜品
                DishVO dishVO = dishService.getById(dishId);
                if(dishVO==null){
                    throw new BaseException("菜品不存在");
                }
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setName(dishVO.getName());

            }else {
                //是套餐
                Setmeal setmeal= setmealService.getById(setmealId);
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
            }
            shoppingCart.setUserId(userId);
            shoppingCart.setDishId(dishId);
            shoppingCart.setDishFlavor(dto.getDishFlavor());
            shoppingCart.setSetmealId(setmealId);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);


        }else {
            //否 数量+1 update
            ShoppingCart shoppingCart1 = new ShoppingCart();


           shoppingCart1.setNumber( shoppingCart.getNumber()+1);
           shoppingCart1.setId(shoppingCart.getId());
           cartMapper.update(shoppingCart1);



        }

    }
    /**
     * 查看购物车
     * */

    @Override
    public List<ShoppingCartVO> list() {
        Long currentId = BaseContext.getCurrentId();
       List<ShoppingCartVO>list= shoppingCartMapper.seleteByUserId(currentId);
       return list;
       }
    /**
     * 清空购物车
     * */

    @Override
    public void clean() {
        Long currentId = BaseContext.getCurrentId();


        shoppingCartMapper.clean(currentId);


    }
}



